package com.bikes.mileagetracker.persistence

import android.support.test.InstrumentationRegistry
import android.support.test.runner.AndroidJUnit4
import com.bikes.mileagetracker.orm.BaseDataBaseHelper
import com.bikes.mileagetracker.entity.TrackingEntity
import com.bikes.mileagetracker.service.PersistenceService
import com.bikes.mileagetracker.utils.TaskUtils
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import java.text.SimpleDateFormat

@RunWith(AndroidJUnit4::class)
class TrackingEntityTest {

    val trackings = listOf<TrackingEntity>(
            TrackingEntity().apply {
                this.endAddress = "endAddress1"
                this.endDate = "2018-10-01"
                this.endLongitude = -122.084
                this.endLatitude = 37.422
                this.endTime = "00:05:00"
                this.endTm = SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS").parse("${this.endDate} ${this.endTime}.000").time
                this.miles = 0
                this.startAddress = "startAddress1"
                this.startDate = "2018-10-01"
                this.startLongitude = -122.084
                this.startLatitude = 37.422
                this.startTime = "00:00:00"
                this.startTm = SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS").parse("${this.startDate} ${this.startTime}.000").time
                this.userId = "1"
            }, TrackingEntity().apply {
        this.endAddress = "endAddress2"
        this.endDate = "2018-10-02"
        this.endLongitude = -122.084
        this.endLatitude = 37.422
        this.endTime = "00:05:00"
        this.endTm = SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS").parse("${this.endDate} ${this.endTime}.000").time
        this.miles = 0
        this.startAddress = "startAddress2"
        this.startDate = "2018-10-02"
        this.startLongitude = -122.084
        this.startLatitude = 37.422
        this.startTime = "00:00:00"
        this.startTm = SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS").parse("${this.startDate} ${this.startTime}.000").time
        this.userId = "1"
    }, TrackingEntity().apply {
        this.endAddress = "endAddress2"
        this.endDate = "2018-10-02"
        this.endLongitude = -122.084
        this.endLatitude = 37.422
        this.endTime = "00:05:00"
        this.endTm = SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS").parse("${this.endDate} ${this.endTime}.000").time
        this.miles = 0
        this.startAddress = "startAddress2"
        this.startDate = "2018-10-02"
        this.startLongitude = -122.084
        this.startLatitude = 37.422
        this.startTime = "00:00:00"
        this.startTm = SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS").parse("${this.startDate} ${this.startTime}.000").time
        this.userId = "2"
    }

    )

    val LOCK = Object()

    fun notifyLock() {
        synchronized(LOCK) {
            LOCK.notify()
        }
    }

    fun lock() {
        synchronized(LOCK) {
            LOCK.wait(60000)
        }
    }

    @Test
    fun testTrackingEntityOps() {

        val appContext = InstrumentationRegistry.getTargetContext()
        Assert.assertEquals("com.bikes.mileagetracker", appContext.packageName)

        BaseDataBaseHelper.createTable(BaseDataBaseHelper.getDatabase()!!, TrackingEntity::class.java, true)

        val helper = object : BaseDataBaseHelper<TrackingEntity>() {}


        while (PersistenceService.service == null) {
            PersistenceService.bind(appContext)
            Thread.sleep(100)
        }

        val service = PersistenceService.service!!

        TaskUtils.runInBackground {
            service.saveTracking(trackings[0])
            notifyLock()
        }
        lock()

        var result = helper.byId(trackings[0].id)
        Assert.assertNotNull(result)
        Assert.assertNotNull(result!!.id)
        Assert.assertEquals(trackings[0].id, result!!.id)


        TaskUtils.runInBackground {
            service.saveTracking(trackings[1])
            notifyLock()
        }
        lock()

        result = helper.byId(trackings[1].id)
        Assert.assertNotNull(result)
        Assert.assertNotNull(result!!.id)
        Assert.assertEquals(trackings[1].id, result!!.id)

        Assert.assertEquals(service.loadUserTrackings(trackings[0].userId).size, 2)


    }
}